package com.basic.modules.ykf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.basic.common.utils.ParamsModel;
import com.basic.common.utils.ParamUtils;
import com.basic.config.DictConfig;
import com.basic.modules.sys.entity.SysUserEntity;
import com.basic.modules.sys.service.SysUserService;
import com.basic.modules.ykf.entity.YkfCompanyDeptEntity;
import com.basic.modules.ykf.entity.YkfCustomGroupEntity;
import com.basic.modules.ykf.entity.YkfDictEntity;
import com.basic.modules.ykf.service.YkfCompanyDeptService;
import com.basic.modules.ykf.service.YkfCustomGroupService;
import com.basic.modules.ykf.service.YkfDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.basic.common.utils.PageUtils;
import com.basic.common.utils.Query;

import com.basic.modules.ykf.dao.YkfDictDetailDao;
import com.basic.modules.ykf.entity.YkfDictDetailEntity;
import com.basic.modules.ykf.service.YkfDictDetailService;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("ykfDictDetailService")
public class YkfDictDetailServiceImpl extends ServiceImpl<YkfDictDetailDao, YkfDictDetailEntity> implements YkfDictDetailService {

    @Lazy
    @Autowired
    private YkfDictService dictService;

    @Lazy
    @Autowired
    private YkfCompanyDeptService companyDeptService;

    @Lazy
    @Autowired
    private YkfCustomGroupService customGroupService;

    @Lazy
    @Autowired
    private SysUserService sysUserService;

    @Override
    public Map<String,  Map<String, String>> getDictMap() {
        Map<String,  Map<String, String>> map = new HashMap<>();
        //查询所有字典
        List<YkfDictEntity> list = dictService.list();
        if (ObjectUtil.isNotEmpty(list)) {
            for (YkfDictEntity ykfDictEntity : list) {
                //查询字典明细
                Map<String, String> detailMap = new HashMap<>();
                QueryWrapper<YkfDictDetailEntity> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(YkfDictDetailEntity::getDictId, ykfDictEntity.getDictId());
                List<YkfDictDetailEntity> detailList = this.list(queryWrapper);
                if (ObjectUtil.isNotEmpty(detailList)) {
                    for (YkfDictDetailEntity detailEntity : detailList) {
                        detailMap.put(detailEntity.getVal(), detailEntity.getName());
                    }
                }
                map.put(ykfDictEntity.getDictNo(), detailMap);
            }
        }
        //查询部门
        List<YkfCompanyDeptEntity> deptList = companyDeptService.list();
        if (ObjectUtil.isNotEmpty(deptList)) {
            Map<String, String> detailMap = new HashMap<>();
            deptList.forEach(dept -> {
                detailMap.put(StrUtil.toString(dept.getDeptId()), dept.getDeptName());
            });
            map.put("department", detailMap);
        } else {
            map.put("department", Collections.emptyMap());
        }
        //查询客户分组
        List<YkfCustomGroupEntity> customGroupList = customGroupService.list();
        if (ObjectUtil.isNotEmpty(customGroupList)) {
            Map<String, String> detailMap = new HashMap<>();
            customGroupList.forEach(customGroup -> {
                detailMap.put(StrUtil.toString(customGroup.getCustomGroupId()), customGroup.getCustomGroupName());
            });
            map.put("customGroup", detailMap);
        } else {
            map.put("customGroup", Collections.emptyMap());
        }
        //查询用户
        List<SysUserEntity> userList = sysUserService.list();
        if (ObjectUtil.isNotEmpty(userList)) {
            Map<String, String> flowerDetailMap = new HashMap<>();
            Map<String, String> nameDetailMap = new HashMap<>();
            userList.forEach(user -> {
                flowerDetailMap.put(StrUtil.toString(user.getUserId()), user.getFlowerName());
                nameDetailMap.put(StrUtil.toString(user.getUserId()), user.getName());
            });
            map.put("flowerName", flowerDetailMap);
            map.put("userName", nameDetailMap);
        } else {
            map.put("flowerName", Collections.emptyMap());
            map.put("userName", Collections.emptyMap());
        }
        return map;
    }

    @Override
    public void updateDictMap(String dictNo, String val, String name) {
        Map<String, String> stringStringMap = DictConfig.DICT_MAPPER.get(dictNo);
        if (ObjectUtil.isNotEmpty(stringStringMap)) {
            stringStringMap.put(val, name);
            DictConfig.DICT_MAPPER.put(dictNo, stringStringMap);
        } else {
            stringStringMap = new HashMap<>();
            stringStringMap.put(val, name);
            DictConfig.DICT_MAPPER.put(dictNo, stringStringMap);
        }
    }

    @Override
    public String getDictName(String dictNo, String val) {
        Map<String, String> stringStringMap = DictConfig.DICT_MAPPER.get(dictNo);
        if (ObjectUtil.isNotEmpty(stringStringMap)) {
            String string = stringStringMap.get(val);
            if (ObjectUtil.isEmpty(string)) {
                return val;
            } else {
                return string;
            }
        } else {
           return val;
        }
    }
}
